<?php
namespace Admin\Controller;
use Think\Controller;
use think\Page;
class BookOrderManageController extends Controller
{
     public function __construct()
     {
         parent::__construct();
         if(!logAdmin(session('adminId'))) header('location:'.U('Admin/Index/login'));
     }

    public function index(){
         
     }
     
     public function Order_list(){
         if(IS_GET){
                      $result=D('BookOrder')->Order_List();
                      $money=D('BookOrder')->Count_money();
                      $this->assign('money',$money);
                      $this->assign('Order_List',$result['list']);
                      $this->assign('page',$result['show']);
                      $this->display('OrderManage/BookOrder_list');
           
//          switch ($len){
//              case 2: 
//                  if($type[0]==1){
          
//                  }                //显示书籍订单列表页面
//                  else  $this->display('OrderManage/OtherOrder_list'); break;   //显示电子资料、题库、押密流水列表页面            
         
//          }
     }
     }  
     
     public function Edit_status(){
         if(IS_GET){
             $order_id=I('get.order_id');            
             if($order_id!=''){
                  $status=D('BookOrder')->Edit_status($order_id);
                  $this->assign('order_id',$status['order_id']);
                  $this->assign('status',$status['status']);
                  $this->display('OrderManage/Edit_status');
             }
         }
     }
     
     public function save_status(){
         if(IS_POST){
             $status=I('post.');
             if(!empty($status)){
                  $result=D('BookOrder')->save_status($status);
                  $result=$result?'修改成功':'修改失败 ';
                  $this->ajaxReturn($result);
             }else{
                  $this->ajaxReturn('提交信息出错');
             }
         }
     }
     
     public function Delete_order(){
         if(IS_POST){
             $info=I('post.');
             if(!empty($info)){
                 $result=D('BookOrder')->delete_order($info);
                 $dele=$result['delCount']?true:false;
                 $money=$result['money'];
                 $data['delCount']=$dele;
                 $data['money']=$money;
                 $this->ajaxReturn($data);
             }else{
                 $this->ajaxReturn('提交信息出错');
             }
         }
     }
     
     public function Delete_all(){
         if(IS_POST){
             $info=I('post.');
             if(!empty($info)){
                 $result=D('BookOrder')->delete_all($info);
                 $dele=$result['delCount']?true:false;
                 $money=$result['money'];
                 $data['delCount']=$dele;
                 $data['money']=$money;
                 $this->ajaxReturn($data);
             }else{
                 $this->ajaxReturn('提交信息出错');
             }
         }
     }
     
     /*
      * 进行订单的筛选查询，order_type为订单类型。数据库中默认的订单日期为 2000-01-01的时间戳
      */
     
     public function query_order(){
          if(IS_GET){
              $data=I('get.');
              if(!empty($data)){ 
                  $list=D('BookOrder')->query_order($data);   
                  if($list['list']){
                       $this->assign('page',$list['show']);
                       $this->assign('money',$list['money']);
                       $this->assign('Order_List',$list['list']);
                       $this->display('OrderManage/BookOrder_list');
                   }
                   else{
                       $this->error('未查找到满足条件的数据');
                   }
              }
          }
     }
     public function OutputOrder(){
         
         Vendor('Classes.PHPExcel');
         Vendor('Classes.PHPExcel.IOFactory');
         Vendor('Classes.PHPExcel.Writer.Excel5');
         
         $objExcel = new \PHPExcel();  // 创建一个处理对象实例
                                      // 创建文件格式写入对象实例, uncomment
         $objWriter = new \PHPExcel_Writer_Excel5($objExcel);
          
         $objExcel->setActiveSheetIndex(0);
         $objActSheet = $objExcel->getActiveSheet();
          
         $objActSheet->setTitle('书籍订单表');
         
         $result=M('bookOrder')->where(['status'=>'1'])->select();
                 
                 $objActSheet->setCellValue('A1', '自动编号');  
                 $objActSheet->setCellValue('B1', '会员昵称');  
                 $objActSheet->setCellValue('C1', '订单编号');  
                 $objActSheet->setCellValue('D1', '单价');  
                 $objActSheet->setCellValue('E1', '数量');  
                 $objActSheet->setCellValue('F1', '总价');  
                 $objActSheet->setCellValue('G1', '订单日期');  
                 $objActSheet->setCellValue('H1', '收货人姓名');  
                 $objActSheet->setCellValue('I1', '电话'); 
                 $objActSheet->setCellValue('J1', '地址');  
                 $objActSheet->setCellValue('K1', '订单状态');  
                 $objActSheet->setCellValue('L1', '订单类型');  
                 $objActSheet->setCellValue('M1', '商品名称');  
                 $objActSheet->setCellValue('N1', '会员身份证编号');  
                
        for($i=0;$i<count($result);$i++){
            
                  $objActSheet->setCellValue('A'.($i+2), $result[$i]['id']);  
                  $objActSheet->setCellValue('B'.($i+2), $result[$i]['nick_name']);  
                  $objActSheet->setCellValue('C'.($i+2), $result[$i]['order_id']);  
                  $objActSheet->setCellValue('D'.($i+2), $result[$i]['single_price']);  
                  $objActSheet->setCellValue('E'.($i+2), $result[$i]['count']);  
                  $objActSheet->setCellValue('F'.($i+2), $result[$i]['total_price']);  
                  $objActSheet->setCellValue('G'.($i+2), date('Y-m-d',$result[$i]['date']));  
                  $objActSheet->setCellValue('H'.($i+2), $result[$i]['consignee']);  
                  $objActSheet->setCellValue('I'.($i+2), $result[$i]['tel']);  
                  $objActSheet->setCellValue('J'.($i+2), $result[$i]['address']);  
                  $objActSheet->setCellValue('K'.($i+2), 已支付);  
                  $objActSheet->setCellValue('L'.($i+2), 书籍订单);  
                  $objActSheet->setCellValue('M'.($i+2), $result[$i]['goods_name']);  
                  $objActSheet->setCellValue('N'.($i+2), $result[$i]['tcard']);
            
        }
         $outputFileName = 'BookOrder'.".xls";
         ob_end_clean();//清除缓冲区,避免乱码
         header("Content-Type: application/force-download");
         header("Content-Type: application/octet-stream");
         header("Content-Type: application/download");
         header('Content-Disposition:attachment;filename="' . $outputFileName . '"');  //到文件
         $objWriter->save('php://output');
     }
}